/*
 *      configuration.c
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#ifndef _IMPL_CONFIG
#define _IMPL_CONFIG

#include "configuration.h"

#ifdef _WIN32
static conf *load_conf(char *argv){
#else
conf *load_conf(char *argv){
#endif
	char *tlsparm,parm_name[100],parm_val[100];
	FILE *conf_file;
	bool localargv=false;
	bool bsrv_ip=false,bsrv_port=false,busr=false,bpw=false,bmy_uri=false;
	bool tls_enable,proceed_on_tls_fail;
	
	c0=(conf *)malloc(sizeof(conf));
	c0->dir_hpsign=NULL;
	c0->dir_sign=NULL;
	c0->dir_react=NULL;
	c0->dir_send=NULL;
	c0->dtd_sign=NULL;
	//c0->certificate=NULL;
	//c0->certificate_key)NULL;
	c0->groups_file=NULL;
	c0->max_hash_entry=100;
	c0->polling_time=30;
	c0->read_retries=10;

	tls_enable=true;
	proceed_on_tls_fail=false;
	
	if(argv == NULL){
		localargv=true;
		argv=(char*)calloc(sizeof(char),strlen(DEFAULT_CONF_FILE));
		strncpy(argv,DEFAULT_CONF_FILE,strlen(DEFAULT_CONF_FILE));
	} 
	if((conf_file=fopen(argv,"rt"))==NULL){
		fprintf(stderr,"Cannot open %s configuration file!\n",argv);
		exit(-1);
	}

	/* Ricerca dei parametri di configurazione nel file */
	while(!feof(conf_file)){
		fscanf(conf_file,"%s = %99[^\n]",parm_name,parm_val);  
		/*"usr = ..."*/
		if(strncasecmp(parm_name,"usr",strlen(parm_name))==0){
			c0->usr=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			busr=true;
			strncpy(c0->usr,parm_val,strlen(parm_val)+1);
		}
		/*"pw = ..."*/
		if(strncasecmp(parm_name,"pw",strlen(parm_name))==0){
			c0->pw=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			bpw=true;
			strncpy(c0->pw,parm_val,strlen(parm_val)+1);		
		}
		/*"srv_ip = ..."*/
		if(strncasecmp(parm_name,"srv_ip",strlen(parm_name))==0){
			bsrv_ip=true;
			c0->srv_ip=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->srv_ip,parm_val,strlen(parm_val)+1);
		}
		/*"srv_port = ..."*/
		if(strncasecmp(parm_name,"srv_port",strlen(parm_name))==0){
			c0->srv_port=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			bsrv_port=true;
			strncpy(c0->srv_port,parm_val,strlen(parm_val)+1);
		}
		/*"directory_hpsign = .."*/
		if(strncasecmp(parm_name,"directory_hpsign",strlen(parm_name))==0){
			c0->dir_hpsign=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dir_hpsign,parm_val,strlen(parm_val)+1);
		}
		/*"directory_react = .."*/
		if(strncasecmp(parm_name,"directory_react",strlen(parm_name))==0){
			c0->dir_react=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dir_react,parm_val,strlen(parm_val)+1);
		}
		/*"directory_send = .."*/
		if(strncasecmp(parm_name,"directory_send",strlen(parm_name))==0){
			c0->dir_send=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dir_send,parm_val,strlen(parm_val)+1);
		}
		/*"directory_sign = .."*/
		if(strncasecmp(parm_name,"directory_sign",strlen(parm_name))==0){
			c0->dir_sign=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dir_sign,parm_val,strlen(parm_val)+1);
		}
		/*"dtd_sign = .."*/
		if(strncasecmp(parm_name,"dtd_sign",strlen(parm_name))==0){
			c0->dtd_sign=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->dtd_sign,parm_val,strlen(parm_val)+1);
		}
		/*"groups_file = .."*/
		if(strncasecmp(parm_name,"groups_file",strlen(parm_name))==0){
			c0->groups_file=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->groups_file,parm_val,strlen(parm_val)+1);
		}
		/*"max_hash_entry = .."*/
		if(strncasecmp(parm_name,"max_hash_entry",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->max_hash_entry);
		}

		/*"certificate_file = .."*/
		if(strncasecmp(parm_name,"certificate_file",strlen(parm_name))==0){
			c0->certificate=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->certificate,parm_val,strlen(parm_val)+1);
		}
		/*"certificate_key = .."*/
		if(strncasecmp(parm_name,"certificate_key",strlen(parm_name))==0){
			c0->certificate_key=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(c0->certificate_key,parm_val,strlen(parm_val)+1);
		}
		/*"polling_time = .."*/
			if(strncasecmp(parm_name,"polling_time",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->polling_time);
		}
		/*"read_retries = .."*/
		if(strncasecmp(parm_name,"read_retries",strlen(parm_name))==0){
			sscanf(parm_val,"%d",&c0->read_retries);
		}
		
		if(strncasecmp(parm_name,"tls_enable",strlen(parm_name))==0){
			tlsparm=(char *)calloc(sizeof(char),strlen(parm_val)+1);
			strncpy(tlsparm,parm_val,strlen(parm_val)+1);
			if(strncasecmp(tlsparm,"true",strlen(tlsparm))!=0){
				tls_enable=false;				
			}
			free(tlsparm);
			/*ADDED*/tlsparm=NULL;
		}
	}
	fclose(conf_file);
	if(!(bsrv_ip && bsrv_port && busr && bpw)){
		printf("Config file error\n");
		exit(-1);
	}

	if(c0->dir_hpsign == NULL){
		c0->dir_hpsign=(char *)calloc(sizeof(char),strlen(HP_DIR)+1);
		strncpy(c0->dir_hpsign,HP_DIR,strlen(HP_DIR)+1);
	}
	if(c0->dir_sign == NULL){
		c0->dir_sign=(char *)calloc(sizeof(char),strlen(NORMAL_DIR)+1);
		strncpy(c0->dir_sign,NORMAL_DIR,strlen(NORMAL_DIR)+1);
	}
	if(c0->dir_react == NULL){
		c0->dir_react=(char *)calloc(sizeof(char),strlen(STOREDIR)+1);
		strncpy(c0->dir_react,STOREDIR,strlen(STOREDIR)+1);
	}
	if(c0->dir_send == NULL){
		c0->dir_send=(char *)calloc(sizeof(char),strlen(SENDDIR)+1);
		strncpy(c0->dir_send,SENDDIR,strlen(SENDDIR)+1);
	}
	if(c0->dtd_sign == NULL){
		c0->dtd_sign=(char *)calloc(sizeof(char),strlen(SIGN_DTD)+1);
		strncpy(c0->dtd_sign,SIGN_DTD,strlen(SIGN_DTD)+1);
	}
	//if(c0->certificate == NULL){
		//c0->certificate=(char *)calloc(sizeof(char),strlen(SIGN_DTD)+1);
		//strncpy(c0->certificate,SIGN_DTD,strlen(SIGN_DTD)+1);
	//}
	//if(c0->certificate_key == NULL){
		//c0->certificate_key=(char *)calloc(sizeof(char),strlen(SIGN_DTD)+1);
		//strncpy(c0->certificate_key,SIGN_DTD,strlen(SIGN_DTD)+1);
	//}

	if(localargv){ free(argv); /*ADDED*/argv=NULL;}
	
	return c0;
}



#endif